<template>
  <transition name="el-zoom-in-center">
    <div class="jsbos-preview-main">
      <div class="jsbos-common-page-header">
        <el-page-header
          @back="goBack"
          :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
        />
        <div class="options">
          <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
            保 存</el-button
          >
          <el-button @click="goBack"> 取 消</el-button>
        </div>
      </div>
      <el-row
        :gutter="15"
        class="main"
        :style="{ margin: '0 auto', width: '1200px' }"
      >
        <el-form
          v-loading="loading"
          ref="elForm"
          :model="dataForm"
          :rules="rules"
          size="small"
          label-width="100px"
          label-position="right"
        >
          <template v-if="!loading">
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="基本信息">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="封面" prop="cover">
                <UploadCropImg
                  v-model="dataForm.cover"
                  sizeUnit="MB"
                  :limit="9"
                >
                </UploadCropImg>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input
                  v-model="dataForm.name"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="目录" prop="categoryId">
                <TreeSelect
                  v-model="dataForm.categoryId"
                  placeholder="请选择"
                  clearable
                  :options="categoryIdOptions"
                  :props="categoryIdProps"
                >
                </TreeSelect>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开始时间" prop="startDatetime">
                <el-date-picker
                  v-model="dataForm.startDatetime"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="datetime"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="截至时间" prop="endDatetime">
                <el-date-picker
                  v-model="dataForm.endDatetime"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="datetime"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="是否关联培训计划" prop="plan">
                <el-switch
                  v-model="dataForm.plan"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="培训计划" prop="planId">
                <custom-select
                  v-model="dataForm.planId"
                  placeholder="请选择"
                  clearable
                >
                </custom-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="计划明细" prop="planDetailId">
                <custom-select
                  v-model="dataForm.planDetailId"
                  placeholder="请选择"
                  clearable
                >
                </custom-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="预算费用" prop="budget">
                <el-input-number
                  v-model="dataForm.budget"
                  placeholder="数字文本"
                  :step="1"
                  controls-position="right"
                >
                </el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="培训形式" prop="type">
                <el-select
                  v-model="dataForm.type"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in typeOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="培训层级" prop="grade">
                <el-radio-group v-model="dataForm.grade" size="small">
                  <el-radio
                    v-for="(item, index) in gradeOptions"
                    :key="index"
                    :label="item.id"
                    :disabled="item.disabled"
                  >
                    {{ item.fullName }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="介绍" prop="description">
                <el-input
                  v-model="dataForm.description"
                  placeholder="请输入介绍"
                  :style="{ width: '100%' }"
                  true
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="阶段任务">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="阶段间闯关" prop="interphaseChallenges">
                <el-switch
                  v-model="dataForm.interphaseChallenges"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="同步学习进度" prop="syncProgress">
                <el-switch
                  v-model="dataForm.syncProgress"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="逾期后可学" prop="overdueLearning">
                <el-switch
                  v-model="dataForm.overdueLearning"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="可见范围">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="可见类型" prop="scopeType">
                <el-radio-group v-model="dataForm.scopeType" size="small">
                  <el-radio
                    v-for="(item, index) in scopeTypeOptions"
                    :key="index"
                    :label="item.id"
                    :disabled="item.disabled"
                  >
                    {{ item.fullName }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="报名设置">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否开启报名" prop="signUp">
                <el-switch
                  v-model="dataForm.signUp"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="报名人数" prop="signNum">
                <el-input-number
                  v-model="dataForm.signNum"
                  placeholder="数字文本"
                  :step="1"
                  controls-position="right"
                >
                </el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="报名开始" prop="signStartDatetime">
                <el-date-picker
                  v-model="dataForm.signStartDatetime"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="报名截至" prop="signEndDatetime">
                <el-date-picker
                  v-model="dataForm.signEndDatetime"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="报名范围" prop="signOraginzeId">
                <com-select
                  v-model="dataForm.signOraginzeId"
                  placeholder="请选择"
                  clearable
                >
                </com-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="是否需要审核" prop="signAudit">
                <el-radio-group v-model="dataForm.signAudit" size="small">
                  <el-radio
                    v-for="(item, index) in signAuditOptions"
                    :key="index"
                    :label="item.id"
                    :disabled="item.disabled"
                  >
                    {{ item.fullName }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="学员端报名中心" prop="studentShow">
                <el-radio-group v-model="dataForm.studentShow" size="small">
                  <el-radio
                    v-for="(item, index) in studentShowOptions"
                    :key="index"
                    :label="item.id"
                    :disabled="item.disabled"
                  >
                    {{ item.fullName }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="文档学习设置">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开启文档学习时长" prop="docsLearning">
                <el-switch
                  v-model="dataForm.docsLearning"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="文档学习时长" prop="docsLearningLength">
                <el-input-number
                  v-model="dataForm.docsLearningLength"
                  placeholder="数字文本"
                  :step="1"
                  controls-position="right"
                >
                </el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="通过条件">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否必修" prop="required">
                <el-switch
                  v-model="dataForm.required"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="必修完成数量" prop="requiredCompleted">
                <el-input-number
                  v-model="dataForm.requiredCompleted"
                  placeholder="数字文本"
                  :step="1"
                  controls-position="right"
                >
                </el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="必修总数" prop="requiredTotal">
                <el-input-number
                  v-model="dataForm.requiredTotal"
                  placeholder="数字文本"
                  :step="1"
                  controls-position="right"
                >
                </el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否选修" prop="elective">
                <el-switch
                  v-model="dataForm.elective"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="选修完成数量" prop="electiveCompleted">
                <el-input-number
                  v-model="dataForm.electiveCompleted"
                  placeholder="数字文本"
                  :step="1"
                  controls-position="right"
                >
                </el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="选修总数" prop="electiveTotal">
                <el-input-number
                  v-model="dataForm.electiveTotal"
                  placeholder="数字文本"
                  :step="1"
                  controls-position="right"
                >
                </el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="学习奖励">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="证书模板" prop="certificateId">
                <el-input
                  v-model="dataForm.certificateId"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="监督人">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="监督人" prop="supervisor">
                <user-select
                  v-model="dataForm.supervisor"
                  placeholder="请选择"
                  clearable
                >
                </user-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="状态" prop="enabledMark">
                <el-select
                  v-model="dataForm.enabledMark"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in enabledMarkOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
        </el-form>
      </el-row>
    </div>
  </transition>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        cover: [],
        name: "",
        categoryId: "",
        startDatetime: "",
        endDatetime: "",
        plan: 0,
        planId: "",
        planDetailId: "",
        budget: 0,
        type: "1",
        grade: "1",
        description: "",
        interphaseChallenges: 0,
        syncProgress: 0,
        overdueLearning: 0,
        scopeType: "1",
        signUp: 0,
        signNum: 0,
        signStartDatetime: "",
        signEndDatetime: "",
        signOraginzeId: [],
        signAudit: "0",
        studentShow: "1",
        docsLearning: 0,
        docsLearningLength: 0,
        required: 0,
        requiredCompleted: 0,
        requiredTotal: 0,
        elective: 0,
        electiveCompleted: 0,
        electiveTotal: 0,
        certificateId: "",
        supervisor: "",
        enabledMark: "0",
        creatorUserId: "",
        creatorTime: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        startDatetime: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        endDatetime: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
      },
      categoryIdOptions: [],
      categoryIdProps: {
        children: "children",
        label: "fullName",
        value: "enCode",
      },
      typeOptions: [
        { fullName: "内部培训", id: "1" },
        { fullName: "外部培训", id: "2" },
        { fullName: "外请培训", id: "3" },
      ],
      gradeOptions: [
        { fullName: "公司级", id: "1" },
        { fullName: "部门级", id: "2" },
      ],
      scopeTypeOptions: [
        { fullName: "全公司", id: "1" },
        { fullName: "仅创建人", id: "2" },
        { fullName: "指定人员", id: "3" },
      ],
      signAuditOptions: [
        { fullName: "需要", id: "1" },
        { fullName: "不需要", id: "0" },
      ],
      studentShowOptions: [
        { fullName: "显示", id: "1" },
        { fullName: "不显示", id: "2" },
      ],
      enabledMarkOptions: [
        { fullName: "未发布", id: "0" },
        { fullName: "已发布", id: "1" },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getCategoryIdOptions();
  },
  mounted() {},
  methods: {
    getCategoryIdOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "KnowledgeCategory",
        })
        .then((res) => {
          this.categoryIdOptions = res;
        });
    },
    goBack() {
      this.$emit("refresh");
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/course/TemsProject/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/course/TemsProject",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/course/TemsProject/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.plan = parseInt(_data.plan);
      _data.interphaseChallenges = parseInt(_data.interphaseChallenges);
      _data.syncProgress = parseInt(_data.syncProgress);
      _data.overdueLearning = parseInt(_data.overdueLearning);
      _data.signUp = parseInt(_data.signUp);
      _data.signOraginzeId = JSON.stringify(_data.signOraginzeId);
      _data.docsLearning = parseInt(_data.docsLearning);
      _data.required = parseInt(_data.required);
      _data.elective = parseInt(_data.elective);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.plan = parseInt(_dataAll.plan);
      _dataAll.interphaseChallenges = parseInt(_dataAll.interphaseChallenges);
      _dataAll.syncProgress = parseInt(_dataAll.syncProgress);
      _dataAll.overdueLearning = parseInt(_dataAll.overdueLearning);
      _dataAll.signUp = parseInt(_dataAll.signUp);
      _dataAll.signOraginzeId = JSON.parse(_dataAll.signOraginzeId);
      _dataAll.docsLearning = parseInt(_dataAll.docsLearning);
      _dataAll.required = parseInt(_dataAll.required);
      _dataAll.elective = parseInt(_dataAll.elective);
      this.dataForm = _dataAll;
    },
  },
};
</script>
